/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package converter;

import entities.User;
import java.net.URI;
import java.util.Date;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlAttribute;
import javax.ws.rs.core.UriBuilder;
import javax.persistence.EntityManager;

/**
 *
 * @author Alex
 */

@XmlRootElement(name = "user")
public class UserConverter {
    private User entity;
    private URI uri;
    private int expandLevel;
  
    /** Creates a new instance of UserConverter */
    public UserConverter() {
        entity = new User();
    }

    /**
     * Creates a new instance of UserConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded@param isUriExtendable indicates whether the uri can be extended
     */
    public UserConverter(User entity, URI uri, int expandLevel, boolean isUriExtendable) {
        this.entity = entity;
        this.uri = (isUriExtendable) ? UriBuilder.fromUri(uri).path(entity.getUsrId() + "/").build() : uri;
        this.expandLevel = expandLevel;
    }

    /**
     * Creates a new instance of UserConverter.
     *
     * @param entity associated entity
     * @param uri associated uri
     * @param expandLevel indicates the number of levels the entity graph should be expanded
     */
    public UserConverter(User entity, URI uri, int expandLevel) {
        this(entity, uri, expandLevel, false);
    }

    /**
     * Getter for usrId.
     *
     * @return value for usrId
     */
    @XmlElement
    public Long getUsrId() {
        return (expandLevel > 0) ? entity.getUsrId() : null;
    }

    /**
     * Setter for usrId.
     *
     * @param value the value to set
     */
    public void setUsrId(Long value) {
        entity.setUsrId(value);
    }

    /**
     * Getter for username.
     *
     * @return value for username
     */
    @XmlElement
    public String getUsername() {
        return (expandLevel > 0) ? entity.getUsername() : null;
    }

    /**
     * Setter for username.
     *
     * @param value the value to set
     */
    public void setUsername(String value) {
        entity.setUsername(value);
    }

    /**
     * Getter for password.
     *
     * @return value for password
     */
    @XmlElement
    public String getPassword() {
        return (expandLevel > 0) ? entity.getPassword() : null;
    }

    /**
     * Setter for password.
     *
     * @param value the value to set
     */
    public void setPassword(String value) {
        entity.setPassword(value);
    }

    /**
     * Getter for lastLogin.
     *
     * @return value for lastLogin
     */
    @XmlElement
    public Date getLastLogin() {
        return (expandLevel > 0) ? entity.getLastLogin() : null;
    }

    /**
     * Setter for lastLogin.
     *
     * @param value the value to set
     */
    public void setLastLogin(Date value) {
        entity.setLastLogin(value);
    }

    /**
     * Getter for email.
     *
     * @return value for email
     */
    @XmlElement
    public String getEmail() {
        return (expandLevel > 0) ? entity.getEmail() : null;
    }

    /**
     * Setter for email.
     *
     * @param value the value to set
     */
    public void setEmail(String value) {
        entity.setEmail(value);
    }

    /**
     * Returns the URI associated with this converter.
     *
     * @return the uri
     */
    @XmlAttribute
    public URI getUri() {
        return uri;
    }

    /**
     * Sets the URI for this reference converter.
     *
     */
    public void setUri(URI uri) {
        this.uri = uri;
    }

    /**
     * Returns the User entity.
     *
     * @return an entity
     */
    @XmlTransient
    public User getEntity() {
        if (entity.getUsrId() == null) {
            UserConverter converter = UriResolver.getInstance().resolve(UserConverter.class, uri);
            if (converter != null) {
                entity = converter.getEntity();
            }
        }
        return entity;
    }

    /**
     * Returns the resolved User entity.
     *
     * @return an resolved entity
     */
    public User resolveEntity(EntityManager em) {
        return entity;
    }
}
